package com.suse.warmstill.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.suse.warmstill.common.enums.ResponseEnum;
import com.suse.warmstill.common.exception.BusinessException;
import com.suse.warmstill.common.model.CallResult;
import com.suse.warmstill.common.model.ListPageModel;
import com.suse.warmstill.service.ResourceService;
import com.suse.warmstill.vo.ConditionVO;
import com.suse.warmstill.vo.ResourceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 黄靖
 * @author 2022-10-20 00:42
 * description:
 **/
@CrossOrigin
@Api("前端资源的操作,，部门老大可以操作")
@RequestMapping("/warmstill/resource")
@RestController
public class PCResourceApi {

    @Autowired
    private ResourceService resourceService;

    @GetMapping("/listsResource")
    @ApiOperation("可以根据resourcename进行分页查询查询")
    public CallResult<ListPageModel<ResourceVo>> listsResource(@Validated ConditionVO conditionVO) {
        ListPageModel<ResourceVo> list = resourceService.listsResource(conditionVO);
        return CallResult.success(list);
    }

    @GetMapping("/getOneById/{resourceId}")
    @ApiOperation("根据resourceId查询单条resource资源")
    public CallResult<ResourceVo> getOneById(@PathVariable("resourceId") Long resourceId) {
        if (ObjectUtils.isEmpty(resourceId)) {
            throw new BusinessException(ResponseEnum.PARAM_ISNULL.getCode(), ResponseEnum.PARAM_ISNULL.getMsg());
        }
        final ResourceVo oneById = resourceService.getOneById(resourceId);

        return CallResult.success(oneById);
    }

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @ApiOperation("插入单条数据")
    @PostMapping("/insertOne")
    public CallResult<Void> insertOne(@Validated @RequestBody ResourceVo resourceVo) {

        resourceService.insertOne(resourceVo);
        return CallResult.success();
    }

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @ApiOperation("物理删除单条或者多条")
    @PostMapping("/deleteOneOrMany")
    public CallResult<Void> deleteOneOrMany(List<Long> rouseIds) {
        if (rouseIds.size() == 0) {
            throw new BusinessException(ResponseEnum.PARAM_ISNULL.getCode(), ResponseEnum.PARAM_ISNULL.getMsg());
        }

        resourceService.deleteOneOrMany(rouseIds);
        return CallResult.success();
    }

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @ApiOperation("修改")
    @PutMapping("/updateOne")
    public CallResult<Void> updateOne(@Validated ResourceVo resourceVo) {
        resourceService.updateOne(resourceVo);
        return CallResult.success();
    }


}
